const app = getApp();

Page({
  data: {
    title: '',
    content: '',
    images: [],
    maxImageCount: 9,
    category: ''
  },

  // 输入标题
  onTitleInput(e) {
    this.setData({
      title: e.detail.value
    });
  },

  // 输入内容
  onContentInput(e) {
    this.setData({
      content: e.detail.value
    });
  },

  // 选择图片
  chooseImage() {
    const remainCount = this.data.maxImageCount - this.data.images.length;
    if (remainCount <= 0) {
      wx.showToast({
        title: '最多选择9张图片',
        icon: 'none'
      });
      return;
    }

    wx.chooseMedia({
      count: remainCount,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const newImages = res.tempFiles.map(file => file.tempFilePath);
        this.setData({
          images: [...this.data.images, ...newImages]
        });
      }
    });
  },

  // 删除图片
  deleteImage(e) {
    const index = e.currentTarget.dataset.index;
    const images = this.data.images;
    images.splice(index, 1);
    this.setData({ images });
  },

  // 预览图片
  previewImage(e) {
    const index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.images[index],
      urls: this.data.images
    });
  },

  // 修改分类选择方法
  selectCategory(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      category: category
    });
  },

  // 发布帖子
  submitPost() {
    if (!this.data.title.trim()) {
      wx.showToast({
        title: '请输入标题',
        icon: 'none'
      });
      return;
    }

    if (!this.data.content.trim()) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });
      return;
    }

    if (!this.data.category) {
      wx.showToast({
        title: '请选择分类',
        icon: 'none'
      });
      return;
    }

    const { userStore } = getApp().globalData;
    const token = userStore.data.token;

    // 先上传图片
    const uploadPromises = this.data.images.map(imagePath => {
      return new Promise((resolve, reject) => {
        wx.uploadFile({
          url: 'http://121.41.13.49:8081/file/upload',
          filePath: imagePath,
          name: 'file',
          header: {
            'authentication': token
          },
          success: (res) => {
            const data = JSON.parse(res.data);
            if (data.code === 1) {
              resolve(data.data);
            } else {
              reject(new Error('上传失败'));
            }
          },
          fail: reject
        });
      });
    });

    wx.showLoading({
      title: '发布中...'
    });

    // 所有图片上传完成后发布帖子
    Promise.all(uploadPromises).then(mediaUrls => {
      wx.request({
        url: 'http://121.41.13.49:8081/post/create',
        method: 'POST',
        header: {
          'content-type': 'application/json',
          'authentication': token
        },
        data: {
          title: this.data.title,
          content: this.data.content,
          mediaList: mediaUrls,
          category: this.data.category
        },
        success: (res) => {
          if (res.data.code === 1) {
            wx.showToast({
              title: '发布成功',
              icon: 'success'
            });
            setTimeout(() => {
              wx.navigateBack();
            }, 1500);
          } else {
            wx.showToast({
              title: '发布失败',
              icon: 'none'
            });
          }
        },
        fail: () => {
          wx.showToast({
            title: '发布失败',
            icon: 'none'
          });
        },
        complete: () => {
          wx.hideLoading();
        }
      });
    }).catch(() => {
      wx.hideLoading();
      wx.showToast({
        title: '图片上传失败',
        icon: 'none'
      });
    });
  }
}); 